#include "videosurface.h"
#include <QVideoSurfaceFormat>
#include <QDebug>

VideoSurface::VideoSurface(QObject *parent):QAbstractVideoSurface (parent)
{
}

VideoSurface::~VideoSurface()
{
}

QList<QVideoFrame::PixelFormat> VideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType /*handleType*/) const
{
    QList<QVideoFrame::PixelFormat> listPixelFormats;
    listPixelFormats.append(QVideoFrame::Format_ARGB32);
    listPixelFormats.append(QVideoFrame::Format_ARGB32_Premultiplied);
    listPixelFormats.append(QVideoFrame::Format_RGB32);
    listPixelFormats.append(QVideoFrame::Format_RGB24);
    listPixelFormats.append(QVideoFrame::Format_RGB565);
    listPixelFormats.append(QVideoFrame::Format_RGB555);
    listPixelFormats.append(QVideoFrame::Format_ARGB8565_Premultiplied);
    listPixelFormats.append(QVideoFrame::Format_BGRA32);
    listPixelFormats.append(QVideoFrame::Format_BGRA32_Premultiplied);
    listPixelFormats.append(QVideoFrame::Format_BGR32);
    listPixelFormats.append(QVideoFrame::Format_BGR24);
    listPixelFormats.append(QVideoFrame::Format_BGR565);
    listPixelFormats.append(QVideoFrame::Format_BGR555);
    listPixelFormats.append(QVideoFrame::Format_BGRA5658_Premultiplied);
    listPixelFormats.append(QVideoFrame::Format_Jpeg);
    listPixelFormats.append(QVideoFrame::Format_CameraRaw);
    listPixelFormats.append(QVideoFrame::Format_AdobeDng);
    return listPixelFormats;
}

bool VideoSurface::present(const QVideoFrame &frame)
{
    if (this->surfaceFormat().pixelFormat() != frame.pixelFormat() || this->surfaceFormat().frameSize() != frame.size())
    {
        setError(IncorrectFormatError);
        stop();
        qDebug()<<"error format";
        return false;
    }
    QVideoFrame cloneFrame(frame);
    emit frameAvailable(cloneFrame);
    return true;
}
